<?php 
require_once('../config.php');
$conn = mysqli_connect($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd,$cfg_dbname);

        $files = array();
        $success = 0;    
        $uid=intval($_REQUEST["uid"]);
		$sql="select * from ph_admin where id='".$uid."'";
        $result=mysqli_query($conn,$sql);
        $row=mysqli_fetch_array($result);
		$sql2="select * from ph_admin where admin='".$row["parent"]."'";
        $result2=mysqli_query($conn,$sql2);
        $row2=mysqli_fetch_array($result2);
		$maxhostsize="";
		$currenthostsize="";
		if($row["thetype"]=="管理员"||$row["thetype"]=="超级管理员"||$row2["thetype"]=="超级管理员"){
		$maxhostsize=$row["maxhostsize"];
		$currenthostsize=$row["currenthostsize"];
		}else{
		$maxhostsize=$row2["maxhostsize"];
		$currenthostsize=$row2["currenthostsize"];
		$uid=$row2["ID"];
		}
        
        foreach ($_FILES as $item) {
            $index = count($files);

            $files[$index]['srcName'] = $item['name'];    
            $files[$index]['error'] = $item['error'];    
            $files[$index]['size'] = $item['size'];        
            $files[$index]['type'] = $item['type'];        
            $files[$index]['success'] = false;            
            $files[$index]['path'] = '';                
			$realsize=(int)($files[$index]['size']/1024);
            if((int)($realsize+$currenthostsize)>$maxhostsize){
			echo "alert('Host Is Full!');";
			exit;
			}else{
			mysqli_query($conn,"update ph_admin set currenthostsize=currenthostsize+".$realsize." where id='".$uid."'");
			}
            
            if($item['error'] != 0) continue;
            
            if(!is_uploaded_file($item['tmp_name'])) {
                $files[$index]['error'] = 8000;
                continue;
            }
            
            $extension = '';
            if(strcmp($item['type'], 'image/jpeg') == 0) {
                $extension = '.jpg';
            }
            else if(strcmp($item['type'], 'image/png') == 0) {
                $extension = '.png';
            }
            else if(strcmp($item['type'], 'image/gif') == 0) {
                $extension = '.gif';
            }
            else {
                
                $substr = strrchr($item['name'], '.');
                if(FALSE == $substr) {
                    $files[$index]['error'] = 8002;
                    continue;
                }

                
                if(strcasecmp($substr, '.jpg') == 0 || strcasecmp($substr, '.jpeg') == 0 || strcasecmp($substr, '.jfif') == 0 || strcasecmp($substr, '.jpe') == 0 ) {
                    $files[$index]['type'] = 'image/jpeg';
                }
                else if(strcasecmp($substr, '.png') == 0) {
                    $files[$index]['type'] = 'image/png';
                }
                else if(strcasecmp($substr, '.gif') == 0) {
                    $files[$index]['type'] = 'image/gif';
                }
                else {
                    $files[$index]['error'] = 8003;
                    continue;
                }
                $extension = $substr;
            }

            
            $md5 = md5_file($item['tmp_name']);
            
            $imageInfo = getimagesize($item['tmp_name']);
            $rawImageWidth = $imageInfo[0];
            $rawImageHeight = $imageInfo[1];

            
			
			
            $path =  'files/'.$uid.'/'.date("Y-m-d",time()).'/';
            
            ensure_writable_dir('../'.$path);
            
            $name = "$md5.0x{$rawImageWidth}x{$rawImageHeight}{$extension}";
            
            $ret = file_exists('../'.$path . $name) ? true : move_uploaded_file($item['tmp_name'],'../'.$path . $name);
            if($ret === false) {
                $files[$index]['error'] = 8004;
                continue;
            }
            else {
                $files[$index]['path'] = '/'.$path . $name;        
                $files[$index]['success'] = true;            
                $files[$index]['width'] = $rawImageWidth;   
                $files[$index]['height'] = $rawImageHeight;   
                $success ++;    
            }
        }

        
        echo json_encode(array(
            'total' => count($files),
            'success' => $success,
            'files' => $files,
        ));
		//echo "alert('host is full');";
/*********************************·Ö¸î*************************************************/
//
/**
* 
*
* @param string $dir
*/
function ensure_writable_dir($dir) {
    if(!file_exists($dir)) {
        mkdir($dir, 0766, true);
        chmod($dir, 0766);
        chmod($dir, 0777);
    }
    else if(!is_writable($dir)) {
        chmod($dir, 0766);
        chmod($dir, 0777);
        if(!is_writable($dir)) {
            throw new FileSystemException("目录 $dir 不可写");
        }
    }
}?>